import React, { PureComponent } from 'react';
import logo from './logo.svg';
import './App.css';
import Login from './routes/login'
import Home from './routes/home'
import Lesson from './routes/lesson'
import Feedback from './routes/feedback'
import StudentFeedbackDetail from './routes/feedback/StudentFeedbackDetail'
import Loader from './components/Loader'
import {withRouter, Redirect, Route, Switch} from 'react-router-dom';
import { getProfile } from './service/api';


class App extends PureComponent {

    constructor(props) {
        super(props);
        this.state = {
            profile: {},
            currentStudent: {},
            loading: false,
        }
    }

    componentDidMount() {
       this.getProfile();
    }


    /**
     * 设置全局的
     */
    setGlobalState = (state) => {
        this.setState({...state});
    }

    /**
     * 获得用户信息
     */
    getProfile = () => {
        this.setState({loading: true})
        getProfile().then(res => {
            if(res.status === 0) {
                this.setState({
                    profile: res.data || {},
                    loading: false,
                    currentStudent: res.data.studentList[0] || {}
                })
                const pathname = this.props.history.location.pathname;
                if(pathname === '/login') {
                    this.props.history.push('/home');
                }
            }else {
                this.gotoLogin();
                this.setState({
                    loading: false
                });
            }
        }).catch(error => {
            console.log('发生了异常:', error)
            this.setState({
                loading: false
            });
           this.gotoLogin();
        })
    }


    /**
     * 跳转到登陆页面
     */
    gotoLogin = () => {
        const pathname = this.props.history.location.pathname;
        if(pathname !== '/login') {
            this.props.history.push('/login');
        }
    }

    render() {
        //组件公共参数
        const { loading } = this.state;
        const props = {...this.props, ...this.state, setGlobalState: this.setGlobalState}
        return (
            <>
                <div className="App">
                    <Switch>
                        <Route exact path="/feedback_detail">
                            <StudentFeedbackDetail {...props}/>
                        </Route>
                        <Route exact path="/feedback">
                            <Feedback {...props}/>
                        </Route>
                        <Route exact path="/login">
                            <Login {...props} getProfile={this.getProfile}/>
                        </Route>
                        <Route exact path="/home">
                            <Home {...props}/>
                        </Route>
                        <Route path="/lesson">
                            <Lesson {...props}/>
                        </Route>
                        <Redirect to="/login"/>
                    </Switch>
                </div>
            </>

        );
    }
}

export default withRouter(App)
